package kz.vokebuz.dataStore;



import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class DbAdapter  extends ContentProvider{

	
	
	
	private SQLiteDatabase db;
	private static DbOpenHelper mDB;
	private Context context;
    private ContentValues values ;

	public DbAdapter(Context context) {
		this.context = context;
		values = new ContentValues();
	}

    public void open(){
    	mDB = new DbOpenHelper(context);
    	db = mDB.getWritableDatabase();
    }
    public void close(){
    	db.close();
    }
	public long addRow(SmsMmsMsg msg) {
		if (values.size()>0) {
			values.clear();
		}

		values.put(SmsMmsMsg.COLUMN_TIMESTAMP, msg.getTimeStamp());
		values.put(SmsMmsMsg.COLUMN_SENDER, msg.getMsgOriginator());
		values.put(SmsMmsMsg.COLUMN_TEXT, msg.getMessageBody());
		//values.put(COLUMN_TIMESTAMP,msg.getTimeStamp());

		// ask the database object to insert the new data
		 
		 return	db.insert(DbOpenHelper.TABLE_SMS, null, values);

	}

    
	public Cursor getAllSms(){
		return db.rawQuery("select 0 as '_id',sender, max(time) as 'time' ," 
				+ " max(msgtext) as 'msgtext' , count(msgtext) as 'countmsg' "
				+ " from smslog group by _id,sender order by time desc", null);
		
		
	}
	
	public Cursor getWellcomeListItems(){
		String SQL = "select 1000 as '_id', 'inbox' as 'categoryid' ," 
			+ " count ( case when (status = 'unread') then 'Y' else null end) as 'isread' , " 
		    + " count (*) as 'total' from smslog union select 1, 'new_folder',0,0 from dual" +
		      " order by _id desc; ";
		return db.rawQuery(SQL,null);
		
	}
	
	
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		mDB = new DbOpenHelper(context);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}
}

